/* 2022.10.18 兼容了微信小程序 */

let run = {}

/**
 * 判断是否授权微信步数功能
 * @date 2022-10-18
 */
run.isAuth = function() {
	return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		uni.getSetting({
			success(res) {
				// 没有获取微信步数权限
				if (!res.authSetting['scope.werun']) {
					// 提前向用户发起授权请求
					uni.authorize({
						scope: 'scope.werun',
						success(res) {
							console.log('authorizeSuccess', res)
							resolve(true)
						},
						fail(res) {
							console.log('authorizeFail', res)
							uni.showModal({
								title: "您已拒绝微信步数权限",
								content: "是否进入权限管理，调整授权？",
								success(res) {
									if (res.confirm) {
										uni.openSetting({
											success(res) {
												resolve(false)
											}
										})
									} else {
										resolve(false)
									}
								}
							})
						},
						complete(res) {
							// console.log('authorizeComplete', res)
						}
					})
				} else {
					resolve(true)
				}
			}
		})

		// #endif
	})
}

/**
 * 获取用户过去三十天微信运动步数
 * @date 2022-10-18
 */
run.getWeRunData = function() {
	return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		// 获取用户过去三十天微信运动步数
		wx.getWeRunData({
			success(res) {
				console.log('getWeRunDataSuccess', res)
				// 拿 encryptedData 到开发者后台解密开放数据
				const encryptedData = res.encryptedData
				// 或拿 cloudID 通过云调用直接获取开放数据
				const cloudID = res.cloudID
				resolve(res)
			},
			fail(err) {
				console.log('getWeRunDataFail', err)
				reject(err)
			}
		})
		// #endif
	})
}

export default run
